

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>geoslurp.datapull.uri &mdash; geoslurp v1.1.0 documentation</title>
  

  
  
    <link rel="shortcut icon" href="../../../_static/favicon.ico"/>
  
  
  

  

  
  
    

  

  
  
    <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  

  

  
        <link rel="index" title="Index"
              href="../../../genindex.html"/>
        <link rel="search" title="Search" href="../../../search.html"/>
    <link rel="top" title="geoslurp v1.1.0 documentation" href="../../../index.html"/>
        <link rel="up" title="Module code" href="../../index.html"/> 

  
  <script src="../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav" role="document">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../index.html" class="icon icon-home"> geoslurp
          

          
          </a>

          
            
            
              <div class="version">
                1.1.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../introduction.html">An introduction to Geoslurp</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../installation.html">Installation of the geoslurp package and setting up the PostgreSQL instance</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../confsettings.html">Configuration</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../examples.html">Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../reference.html">Reference Documentation</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">geoslurp</a>
        
      </nav>


      
      <div class="wy-nav-content">
        <div class="rst-content">
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>geoslurp.datapull.uri</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for geoslurp.datapull.uri</h1><div class="highlight"><pre>
<span></span><span class="c1"># This file is part of geoslurp.</span>
<span class="c1"># geoslurp is free software; you can redistribute it and/or</span>
<span class="c1"># modify it under the terms of the GNU Lesser General Public</span>
<span class="c1"># License as published by the Free Software Foundation; either</span>
<span class="c1"># version 3 of the License, or (at your option) any later version.</span>

<span class="c1"># geoslurp is distributed in the hope that it will be useful,</span>
<span class="c1"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c1"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU</span>
<span class="c1"># Lesser General Public License for more details.</span>

<span class="c1"># You should have received a copy of the GNU Lesser General Public</span>
<span class="c1"># License along with Frommle; if not, write to the Free Software</span>
<span class="c1"># Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA</span>

<span class="c1"># Author Roelof Rietbroek (roelof@geod.uni-bonn.de), 2018</span>

<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">datetime</span>
<span class="kn">import</span> <span class="nn">pycurl</span><span class="o">,</span><span class="nn">re</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">from</span> <span class="nn">io</span>  <span class="k">import</span> <span class="n">BytesIO</span>
<span class="kn">from</span> <span class="nn">geoslurp.config.slurplogger</span> <span class="k">import</span> <span class="n">slurplog</span>
<span class="kn">import</span> <span class="nn">gzip</span> <span class="k">as</span> <span class="nn">gz</span>


<div class="viewcode-block" id="findFiles"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.uri.findFiles">[docs]</a><span class="k">def</span> <span class="nf">findFiles</span><span class="p">(</span><span class="nb">dir</span><span class="p">,</span><span class="n">pattern</span><span class="p">,</span><span class="n">since</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Generator to recursively search adirecctor (returns a generator)&quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="n">since</span><span class="p">:</span>
        <span class="n">since</span><span class="o">=</span><span class="n">datetime</span><span class="o">.</span><span class="n">min</span>

    <span class="k">for</span> <span class="n">dpath</span><span class="p">,</span><span class="n">dnames</span><span class="p">,</span><span class="n">files</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">walk</span><span class="p">(</span><span class="nb">dir</span><span class="p">):</span>
        <span class="c1"># for subdir in dnames:</span>
        <span class="c1">#     yield from findFiles(os.path.join(dir,subdir),pattern)</span>

        <span class="k">for</span> <span class="n">file</span> <span class="ow">in</span> <span class="n">files</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">pattern</span><span class="p">,</span><span class="n">file</span><span class="p">):</span>
                <span class="n">fullp</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">dpath</span><span class="p">,</span><span class="n">file</span><span class="p">)</span>

                <span class="k">if</span> <span class="n">since</span> <span class="o">&lt;</span> <span class="n">datetime</span><span class="o">.</span><span class="n">fromtimestamp</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">getmtime</span><span class="p">(</span><span class="n">fullp</span><span class="p">)):</span>
                    <span class="k">yield</span> <span class="n">fullp</span></div>

<div class="viewcode-block" id="timeFromStamp"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.uri.timeFromStamp">[docs]</a><span class="k">def</span> <span class="nf">timeFromStamp</span><span class="p">(</span><span class="n">stamp</span><span class="p">):</span>
    <span class="n">tinfo</span> <span class="o">=</span> <span class="n">stamp</span>

    <span class="k">if</span> <span class="n">tinfo</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
        <span class="n">dt</span><span class="o">=</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">t0</span><span class="o">=</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="mi">1970</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span>
        <span class="n">dt</span><span class="o">=</span><span class="n">t0</span><span class="o">+</span><span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="n">tinfo</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">dt</span></div>

<div class="viewcode-block" id="setFtime"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.uri.setFtime">[docs]</a><span class="k">def</span> <span class="nf">setFtime</span><span class="p">(</span><span class="n">file</span><span class="p">,</span><span class="n">modTime</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;change modification and access time of a file&quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">modTime</span><span class="p">:</span>
        <span class="n">mtime</span><span class="o">=</span><span class="n">time</span><span class="o">.</span><span class="n">mktime</span><span class="p">(</span><span class="n">modTime</span><span class="o">.</span><span class="n">timetuple</span><span class="p">())</span>
        <span class="n">os</span><span class="o">.</span><span class="n">utime</span><span class="p">(</span><span class="n">file</span><span class="p">,(</span><span class="n">mtime</span><span class="p">,</span><span class="n">mtime</span><span class="p">))</span></div>

<div class="viewcode-block" id="curlDownload"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.uri.curlDownload">[docs]</a><span class="k">def</span> <span class="nf">curlDownload</span><span class="p">(</span><span class="n">url</span><span class="p">,</span><span class="n">fileorfid</span><span class="p">,</span><span class="n">mtime</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span><span class="n">gzip</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span><span class="n">gunzip</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span><span class="n">auth</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Download  the content of an url to an open file or buffer using pycurl</span>
<span class="sd">    :param url: url to download from</span>
<span class="sd">    :param fileorfid: filename or open file or buffer</span>
<span class="sd">    :param mtimee: explicitly set the modification time to this (usefull when modification times are not supported</span>
<span class="sd">    b the server)</span>
<span class="sd">    :param gzip: additionally gzip the file on disk (note this routine does not append \*.gz to the file name)</span>
<span class="sd">    :param gunzip: automatically gunzip the downloaded file</span>
<span class="sd">    :param auth: supply authentification data (user and passw)</span>
<span class="sd">    :return: modification time of remote file</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="n">gzip</span> <span class="ow">and</span> <span class="n">gunzip</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;cannot gzip and gunzip at the same time&quot;</span><span class="p">)</span>

    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">fileorfid</span><span class="p">)</span> <span class="o">==</span> <span class="nb">str</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">gunzip</span><span class="p">:</span>
            <span class="n">tmpfile</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">fileorfid</span><span class="p">),</span><span class="s2">&quot;.&quot;</span><span class="o">+</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">fileorfid</span><span class="p">)</span><span class="o">+</span><span class="s2">&quot;.tmp.gz&quot;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">tmpfile</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">fileorfid</span><span class="p">),</span><span class="s2">&quot;.&quot;</span><span class="o">+</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">fileorfid</span><span class="p">)</span><span class="o">+</span><span class="s2">&quot;.tmp&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">gzip</span><span class="p">:</span>
            <span class="c1">#note this routine does not change the filename!!</span>
            <span class="n">fid</span><span class="o">=</span><span class="n">gz</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">tmpfile</span><span class="p">,</span><span class="s1">&#39;wb&#39;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">fid</span><span class="o">=</span><span class="nb">open</span><span class="p">(</span><span class="n">tmpfile</span><span class="p">,</span><span class="s1">&#39;wb&#39;</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">fid</span><span class="o">=</span><span class="n">fileorfid</span>

    <span class="n">crl</span><span class="o">=</span><span class="n">pycurl</span><span class="o">.</span><span class="n">Curl</span><span class="p">()</span>
    <span class="n">crl</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">pycurl</span><span class="o">.</span><span class="n">URL</span><span class="p">,</span><span class="n">url</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">,</span><span class="s1">&#39;%20&#39;</span><span class="p">))</span>
    <span class="n">crl</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">pycurl</span><span class="o">.</span><span class="n">FOLLOWLOCATION</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
    <span class="n">crl</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">pycurl</span><span class="o">.</span><span class="n">WRITEDATA</span><span class="p">,</span><span class="n">fid</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">auth</span><span class="p">:</span>
        <span class="c1">#use authentification</span>
        <span class="n">crl</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">pycurl</span><span class="o">.</span><span class="n">USERPWD</span><span class="p">,</span><span class="n">auth</span><span class="o">.</span><span class="n">user</span><span class="o">+</span><span class="s2">&quot;:&quot;</span><span class="o">+</span><span class="n">auth</span><span class="o">.</span><span class="n">passw</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">crl</span><span class="o">.</span><span class="n">perform</span><span class="p">()</span>
    <span class="k">except</span> <span class="n">pycurl</span><span class="o">.</span><span class="n">error</span> <span class="k">as</span> <span class="n">pyexc</span><span class="p">:</span>
        <span class="c1"># possibly remove a partly downloaded file</span>
        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">tmpfile</span><span class="p">):</span>
            <span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">tmpfile</span><span class="p">)</span>
        <span class="k">raise</span> <span class="n">pyexc</span>

    <span class="n">modtime</span><span class="o">=</span><span class="n">timeFromStamp</span><span class="p">(</span><span class="n">crl</span><span class="o">.</span><span class="n">getinfo</span><span class="p">(</span><span class="n">pycurl</span><span class="o">.</span><span class="n">INFO_FILETIME</span><span class="p">))</span>
    <span class="k">if</span> <span class="n">mtime</span><span class="p">:</span>
        <span class="c1">#force the modification time to that provided</span>
        <span class="n">modtime</span><span class="o">=</span><span class="n">mtime</span>

    <span class="c1">#close file if input was a filename or unzip data in the output file</span>
    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">fileorfid</span><span class="p">)</span> <span class="o">==</span> <span class="nb">str</span><span class="p">:</span>
        <span class="n">fid</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">gunzip</span><span class="p">:</span>
            <span class="c1">#decompress the temporary gzipped file in the outputfile</span>
            <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">fileorfid</span><span class="p">,</span><span class="s1">&#39;wb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fidout</span><span class="p">:</span>
                <span class="k">with</span> <span class="n">gz</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">tmpfile</span><span class="p">,</span><span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">gzid</span><span class="p">:</span>
                    <span class="n">fidout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">gzid</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
            <span class="c1">#remove the temporary file</span>
            <span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">tmpfile</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># just rename temporary file</span>
            <span class="n">os</span><span class="o">.</span><span class="n">rename</span><span class="p">(</span><span class="n">tmpfile</span><span class="p">,</span><span class="n">fileorfid</span><span class="p">)</span>
        <span class="n">setFtime</span><span class="p">(</span><span class="n">fileorfid</span><span class="p">,</span><span class="n">modtime</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">modtime</span></div>


<div class="viewcode-block" id="UriBase"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.uri.UriBase">[docs]</a><span class="k">class</span> <span class="nc">UriBase</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;Base class to store uri resource&quot;&quot;&quot;</span>
    <span class="n">url</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">lastmod</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">auth</span><span class="o">=</span><span class="kc">None</span> <span class="c1">#link to a certain authentification alias</span>
    <span class="n">subdirs</span><span class="o">=</span><span class="s1">&#39;&#39;</span> <span class="c1">#create these subdrectories when downloading the file</span>
    <span class="n">basedir</span><span class="o">=</span><span class="s1">&#39;&#39;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">url</span><span class="p">,</span><span class="n">lastmod</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span><span class="n">auth</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span><span class="n">subdirs</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span><span class="n">basedir</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">url</span><span class="o">=</span><span class="n">url</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lastmod</span><span class="o">=</span><span class="n">lastmod</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">auth</span><span class="o">=</span><span class="n">auth</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">subdirs</span><span class="o">=</span><span class="n">subdirs</span>

<div class="viewcode-block" id="UriBase.updateModTime"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.uri.UriBase.updateModTime">[docs]</a>    <span class="k">def</span> <span class="nf">updateModTime</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Tries to retrieve the last modification time of a file</span>
<span class="sd">        Note: his is often not supported by the server&quot;&quot;&quot;</span>
        <span class="n">crl</span><span class="o">=</span><span class="n">pycurl</span><span class="o">.</span><span class="n">Curl</span><span class="p">()</span>
        <span class="n">crl</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">pycurl</span><span class="o">.</span><span class="n">URL</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">url</span><span class="p">)</span>
        <span class="c1">#note: not all servers support this query with NOBODY set to 1</span>
        <span class="n">crl</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">pycurl</span><span class="o">.</span><span class="n">NOBODY</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">crl</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">crl</span><span class="o">.</span><span class="n">WRITEFUNCTION</span><span class="p">,</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="kc">None</span><span class="p">)</span>
        <span class="n">crl</span><span class="o">.</span><span class="n">perform</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lastmod</span><span class="o">=</span><span class="n">timeFromStamp</span><span class="p">(</span><span class="n">crl</span><span class="o">.</span><span class="n">getinfo</span><span class="p">(</span><span class="n">pycurl</span><span class="o">.</span><span class="n">INFO_FILETIME</span><span class="p">))</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">lastmod</span></div>

<div class="viewcode-block" id="UriBase.download"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.uri.UriBase.download">[docs]</a>    <span class="k">def</span> <span class="nf">download</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">direc</span><span class="p">,</span><span class="n">check</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span><span class="n">gzip</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span><span class="n">gunzip</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span><span class="n">outfile</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span><span class="n">continueonError</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Download file into directory and possibly check the modification time</span>
<span class="sd">        :param check : check whether the file needs updating</span>
<span class="sd">        :param gzip: additionally gzips the file (adds .gz to file name)</span>
<span class="sd">        :param continueonError (bool): don&#39;t raise an exception when a download error occurrs</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c1">#setup the output uri</span>
        <span class="k">if</span> <span class="n">outfile</span><span class="p">:</span>
            <span class="n">outf</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">direc</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">subdirs</span><span class="p">,</span><span class="n">outfile</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">gzip</span><span class="p">:</span>
                <span class="n">outf</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">direc</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">subdirs</span><span class="p">,</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">url</span><span class="p">))</span><span class="o">+</span><span class="s1">&#39;.gz&#39;</span>
            <span class="k">elif</span> <span class="n">gunzip</span><span class="p">:</span>
                <span class="c1">#strip gz suffix</span>
                <span class="n">outf</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">direc</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">subdirs</span><span class="p">,</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">url</span><span class="p">)))[</span><span class="mi">0</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">outf</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">direc</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">subdirs</span><span class="p">,</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">url</span><span class="p">))</span>



        <span class="c1">#create directory if it does not exist</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">outf</span><span class="p">)):</span>
            <span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">outf</span><span class="p">))</span>

        <span class="n">uri</span><span class="o">=</span><span class="n">UriFile</span><span class="p">(</span><span class="n">url</span><span class="o">=</span><span class="n">outf</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">check</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">lastmod</span> <span class="ow">and</span> <span class="n">uri</span><span class="o">.</span><span class="n">lastmod</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">lastmod</span> <span class="o">&lt;=</span> <span class="n">uri</span><span class="o">.</span><span class="n">lastmod</span><span class="p">:</span>
                <span class="c1">#no need to download the file</span>
                <span class="n">slurplog</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Already Downloaded, skipping </span><span class="si">%s</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">uri</span><span class="o">.</span><span class="n">url</span><span class="p">))</span>
                <span class="k">return</span> <span class="n">uri</span><span class="p">,</span><span class="kc">False</span>
        <span class="n">slurplog</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Downloading </span><span class="si">%s</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">uri</span><span class="o">.</span><span class="n">url</span><span class="p">))</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">lastmod</span><span class="p">:</span>
                <span class="n">curlDownload</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">url</span><span class="p">,</span><span class="n">uri</span><span class="o">.</span><span class="n">url</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">lastmod</span><span class="p">,</span><span class="n">gzip</span><span class="o">=</span><span class="n">gzip</span><span class="p">,</span><span class="n">gunzip</span><span class="o">=</span><span class="n">gunzip</span><span class="p">,</span><span class="n">auth</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">auth</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">lastmod</span><span class="o">=</span><span class="n">curlDownload</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">url</span><span class="p">,</span><span class="n">uri</span><span class="o">.</span><span class="n">url</span><span class="p">,</span><span class="n">gzip</span><span class="o">=</span><span class="n">gzip</span><span class="p">,</span><span class="n">gunzip</span><span class="o">=</span><span class="n">gunzip</span><span class="p">,</span><span class="n">auth</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">auth</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">pycurl</span><span class="o">.</span><span class="n">error</span> <span class="k">as</span> <span class="n">pyexc</span><span class="p">:</span>
            <span class="n">slurplog</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Download failed, skipping </span><span class="si">%s</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">uri</span><span class="o">.</span><span class="n">url</span><span class="p">))</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">continueonError</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">pyexc</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">e</span>
        <span class="n">uri</span><span class="o">.</span><span class="n">lastmod</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">lastmod</span>
        <span class="k">return</span> <span class="n">uri</span><span class="p">,</span><span class="kc">True</span></div>

<div class="viewcode-block" id="UriBase.buffer"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.uri.UriBase.buffer">[docs]</a>    <span class="k">def</span> <span class="nf">buffer</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Download file into a buffer (default uses curl)&quot;&quot;&quot;</span>
        <span class="n">buf</span><span class="o">=</span><span class="n">BytesIO</span><span class="p">()</span>
        <span class="n">curlDownload</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">url</span><span class="p">,</span><span class="n">buf</span><span class="p">,</span><span class="n">auth</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">auth</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">buf</span></div></div>

<div class="viewcode-block" id="UriFile"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.uri.UriFile">[docs]</a><span class="k">class</span> <span class="nc">UriFile</span><span class="p">(</span><span class="n">UriBase</span><span class="p">):</span>
        <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">url</span><span class="p">,</span><span class="n">lastmod</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
            <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">url</span><span class="p">,</span><span class="n">lastmod</span><span class="p">)</span>
            <span class="c1">#Lets set lastmod straight away if the file exists</span>
            <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">url</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">lastmod</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">updateModTime</span><span class="p">()</span>
            

<div class="viewcode-block" id="UriFile.updateModTime"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.uri.UriFile.updateModTime">[docs]</a>        <span class="k">def</span> <span class="nf">updateModTime</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lastmod</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">fromtimestamp</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">getmtime</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">url</span><span class="p">))</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">lastmod</span></div>

<div class="viewcode-block" id="UriFile.buffer"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.uri.UriFile.buffer">[docs]</a>        <span class="k">def</span> <span class="nf">buffer</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">pass</span></div></div>


</pre></div>

           </div>
           <div class="articleComments">
            
           </div>
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, Roelof Rietbroek.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../',
            VERSION:'v1.1.0',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../_static/doctools.js"></script>

  

  
  
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>
  

  
  
  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.StickyNav.enable();
      });
  </script>
   

</body>
</html>